package com.atguigu.gmall.game;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 */
class TankWar extends JFrame {
    private static final long serialVersionUID = 1L;
    private MyPanel mp = null;

    public static void main(String[] args) {
        new TankWar();
    }

    public TankWar() {
        mp = new MyPanel();
        this.add(mp);
        this.setSize(800, 600);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.addKeyListener(mp);
    }
}

class MyPanel extends JPanel implements KeyListener {
    private static final long serialVersionUID = 1L;
    private Tank myTank = null;

    public MyPanel() {
        myTank = new Tank(50, 50);
    }

    public void paint(Graphics g) {
        super.paint(g);
        g.fillRect(0, 0, 800, 600);
        myTank.draw(g);
    }

    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_UP) {
            myTank.moveUp();
        } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
            myTank.moveDown();
        } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            myTank.moveLeft();
        } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            myTank.moveRight();
        }
        this.repaint();
    }

    public void keyTyped(KeyEvent e) {
    }

    public void keyReleased(KeyEvent e) {
    }
}

class Tank {
    private int x = 50;
    private int y = 50;
    private int speed = 10;

    public Tank(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public void draw(Graphics g) {
        g.setColor(Color.GREEN);
        g.fillRect(x, y, 30, 30);
    }

    public void moveUp() {
        y -= speed;
    }

    public void moveDown() {
        y += speed;
    }

    public void moveLeft() {
        x -= speed;
    }

    public void moveRight() {
        x += speed;
    }
}

